package org.xq.softcup.mq.serialize;

import org.xq.softcup.mq.serialize.impl.*;
import org.xq.softcup.mq.util.XQRpcException;

public abstract class Serializer {

    public abstract <T> byte[] serialize(T paramT);

    public abstract <T> Object deserialize(byte[] paramArrayOfByte, Class<T> paramClass);

    public static enum SerializeEnum {
        HESSIAN(HessianSerializer.class), HESSIAN1(Hessian1Serializer.class), PROTOSTUFF(ProtostuffSerializer.class), JACKSON(JacksonSerializer.class);

        private Class<? extends Serializer> serializerClass;

        private SerializeEnum(Class<? extends Serializer> serializerClass) {
            this.serializerClass = serializerClass;
        }

        public Serializer getSerializer() {
            try {
                return (Serializer) this.serializerClass.newInstance();
            } catch (Exception e) {
                throw new XQRpcException(e);
            }
        }

        public static SerializeEnum match(String name, SerializeEnum defaultSerializer) {
            for (SerializeEnum item : SerializeEnum.values()) {
                if (item.name().equals(name)) {
                    return item;
                }
            }
            return defaultSerializer;
        }
    }
}
